﻿// ----------------------------------------------------------------------------------
// Sample Code from Fernando Machado Píriz's blog: http://fernandomachadopiriz.com
//
// <copyright company="Fernando Machado Píriz" file="ScreensaverViewModel.cs">
// Copyright (c) Fernando Machado Píriz. Distributed under FSF's GPL v2.</copyright>
// ----------------------------------------------------------------------------------
// This code and information are provided "as is" without warranty of any kind,
// either expressed of implied, including but not limited to the implied warranties
// of merchantability and/or fitness for a particular purpose.
// ----------------------------------------------------------------------------------
namespace Screensaver
{
    using System.Configuration;
    using System.Diagnostics.CodeAnalysis;
    using GivingAPresentation.Commons;

    /// <summary>
    /// The view model of the <code>DesktopBackgroundView</code> using the Model View View Model pattern. An instance of this class is
    /// automatically created by the view as a window resource.
    /// </summary>
    public class ScreensaverViewModel : BaseViewModel
    {
        /// <summary>
        /// A value indicating if desktop icons were disabled or not at the begining of the presentation.
        /// </summary>
        private bool changed;

        /// <summary>
        /// The name of the screensaver active at the begining of the preseentation.
        /// </summary>
        private string screenSaver;

        /// <summary>
        /// Backend field for <code>Model</code> property.
        /// </summary>
        private ScreensaverModel model = new ScreensaverModel();

        /// <summary>
        /// Backend field for <code>View</code> property.
        /// </summary>
        private ScreensaverView view;

        /// <summary>
        /// Gets the model associated with this view model.
        /// </summary>
        public ScreensaverModel Model
        {
            get
            {
                return this.model;
            }
        }

        /// <summary>
        /// Sets the view associated with this view model.
        /// </summary>
        /// <param name="value">The view to be associated with this view model.</param>
        internal void AssignView(ScreensaverView value)
        {
            this.view = value;
        }

        /// <summary>
        /// Closes or terminates the selected programs if the check box checkBoxCloseThesePrograms is checked.
        /// </summary>
        internal void DoStartPresentation()
        {
            if (this.model.Presenting)
            {
                return;
            }

            this.model.Presenting = true;
            this.view.IsEnabled = false;

            // Deactivates the screen saver if it must be turned off remembering if the settings was changed of not to restore it later
            if (this.model.TurnOff && (ScreensaverHelper.Value != null))
            {
                this.changed = true;
                this.screenSaver = ScreensaverHelper.Value;
                ScreensaverHelper.Value = null;
            }
            else
            {
                this.changed = false;
            }
        }

        /// <summary>
        /// Starts the selected programs if the check box checkBoxCloseThesePrograms is checked.
        /// </summary>
        internal void DoStopPresentation()
        {
            if (!this.model.Presenting)
            {
                return;   
            }

            // Reactivates the screen saver if it must be turned off and the original setting was changed before
            if (this.model.TurnOff && this.changed)
            {
                ScreensaverHelper.Value = this.screenSaver;
            }

            this.model.Presenting = false;
            this.view.IsEnabled = true;
        }

        /// <summary>
        /// Loads model state.
        /// </summary>
        [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters",
            MessageId = "GivingAPresentation.Commons.ExceptionHelper.Check(System.Boolean,System.String)",
            Justification = "Resources will be used in a future version.")]
        internal void DoInitializePart()
        {
            ExceptionHelper.Check(!this.model.Presenting, "Cannot load settings while giving a presentation.");

            try
            {
                this.model.Load();
            }
            catch (ConfigurationErrorsException e)
            {
                throw new GivingAPresentationException("Cannot load configuration.", e);
            }
        }

        /// <summary>
        /// Stores model state.
        /// </summary>                                                                   
        [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters",
            MessageId = "GivingAPresentation.Commons.ExceptionHelper.Check(System.Boolean,System.String)",
            Justification = "Resources will be used in a future version.")]
        internal void DoFinalizePart()
        {
            ExceptionHelper.Check(!this.model.Presenting, "Cannot save settings while giving a presentation.");

            try
            {
                this.model.Save();
            }
            catch (ConfigurationErrorsException e)
            {
                throw new GivingAPresentationException("Cannot save configuration.", e);
            }
        }
    }
}
